﻿//************************************************************************************************
// Copyright © 2012 Steven M. Cohn. All Rights Reserved.
//
// Facility:	Microsoft Development Environment 2012  Version 11.0.50727.1
// Environment:	Microsoft .NET Framework 4.5            Version v4.0.30319
//
// Description: Enumeration of cookie flags
//
// Revision History:
// -Who--------------- -When---------- -What------------------------------------------------------
// Steven M. Cohn		27-Nov-2012		New
//************************************************************************************************

namespace CookieChipper
{
	using System;


	#region Known flags
	/*
	 * INTERNET_COOKIE_IS_SECURE = 0x01
	 * INTERNET_COOKIE_IS_SESSION = 0x02
	 * INTERNET_COOKIE_THIRD_PARTY = 0x10
	 * INTERNET_COOKIE_PROMPT_REQUIRED = 0x20
	 * INTERNET_COOKIE_EVALUATE_P3P = 0x40
	 * INTERNET_COOKIE_APPLY_P3P = 0x80
	 * INTERNET_COOKIE_P3P_ENABLED = 0x100
	 * INTERNET_COOKIE_IS_RESTRICTED = 0x200
	 * INTERNET_COOKIE_IE6 = 0x400
	 * INTERNET_COOKIE_IS_LEGACY = 0x800

	 .3....:....2....:....1....:....0
	 10000000000000000000010001010000

	  0 - secure
	  1
	  2
	  3
	  4 - server
	  5
	  6 - client
	  7
	  8
	  9
	 10 - ?
	 11
	 12
	 13
	 14
	 15
	 16
	 17
	 18
	 19
	 20 -> CookieChipper Custom: primary cookie
	 21 -> CookieChipper Custom: expired/expiring
	 22
	 23
	 24
	 25
	 26
	 27
	 28
	 29
	 30
	 31 - ?
	*/
	#endregion Known flags


	/// <summary>
	/// Enumeration of cookie flags.
	/// </summary>

	[Flags]
	internal enum ChipFlags : uint
	{
		Secure = (1 << 0),
		Server = (1 << 4),
		Client = (1 << 6),
		Primary = (1 << 20),
		Expire = (1 << 21)
	}


	/// <summary>
	/// This class adds extension methods to the ChipFlags enum type.
	/// </summary>

	internal static class ChipFlagsExtension
	{

		// This is a combination of all known individual bits OR'd together, excluding the
		// Client and Server bits; those are handled as a special case.

		private const uint KnownValues = (uint)(ChipFlags.Secure);


		/// <summary>
		/// Convert the ChipFlags value to a comma-delimeted string of names.
		/// </summary>
		/// <param name="flags"></param>
		/// <returns></returns>

		public static string ToNames (this ChipFlags flags)
		{
			string names = (flags & ChipFlags.Client) == ChipFlags.Client
				? "Client"
				: "Server";

			uint value = (uint)flags & KnownValues;
			if (value == 0)
			{
				return names;
			}

			return names += ", " + ((ChipFlags)value).ToString("G");
		}
	}
}
